This document demonstrates how to create an interactive map of whisky bars in Hong Kong using the leaflet and tidygeocoder packages in R. We will start by creating a dataset of whisky bars, then geocode their addresses to obtain latitude and longitude coordinates, and finally create a map with custom markers, popups, and labels.
1 Data and Libraries
First, we load the necessary R libraries and create a tibble containing the names, addresses, and logo URLs for a few whisky bars in Hong Kong.
Code
# Load necessary librarieslibrary(tidyverse) # For data manipulationlibrary(rvest) # For web scraping (not used here, but good to have)library(chromote) # For controlling headless Chrome (not used here)library(janitor) # For data cleaninglibrary(tidygeocoder) # For geocoding addresseslibrary(leaflet) # For creating interactive mapslibrary(sf) # For working with spatial data
Code
# Create vectors for bar names, addresses, and logo URLsbar_name=c('Maltcask@太子','House Welley@中環','Casky@灣仔')bar_address=c('169 Sai Yeung Choi St N, Mong Kok,Hongkong','97 Wellington St, Central,Hongkong','20-24 Lockhart Road, Wan Chai,Hongkong')bar_logo=c('https://img.shoplineapp.com/media/image_clips/62f16f8d7df01b0025a82bad/original.png','https://static.wixstatic.com/media/2d390e_27d0b3da21a94459bccf741600782591~mv2.jpg/v1/fill/w_160,h_160,al_c,q_80,usm_0.66_1.00_0.01,enc_auto/Grey.jpg','https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcQsSelY2PRVxaxyU_Ydf9CHgtUrClzpxpqJ2g&s')# Create a tibble (a modern data frame)data001=tibble(bar_name,bar_address,bar_logo)
2 Geocoding Addresses
To place the bars on a map, we need their geographical coordinates. The tidygeocoder package provides a convenient way to convert addresses into latitude and longitude using services like OpenStreetMap (osm).
Code
# Use the geocode function to find coordinates for the addressesdata002 =data001 %>%geocode(bar_address, method ='osm', lat = latitude , long = longitude)# Display the structure of the resulting data frameglimpse(data002)
Rows: 3
Columns: 5
$ bar_name <chr> "Maltcask@太子", "House Welley@中環", "Casky@灣仔"
$ bar_address <chr> "169 Sai Yeung Choi St N, Mong Kok,Hongkong", "97 Wellingt…
$ bar_logo <chr> "https://img.shoplineapp.com/media/image_clips/62f16f8d7df…
$ latitude <dbl> 22.32588, 22.28360, 22.27818
$ longitude <dbl> 114.1686, 114.1543, 114.1714
3 Creating the Map
Now that we have the coordinates, we can create an interactive map using the leaflet package. We will explore three different ways to mark the locations on the map.
3.1 Map 1: Markers with Custom Icons
We can use the bar logos as custom icons for the markers on the map.
Code
# Create a list of custom icons from the logo URLsleafIcons <-icons(data002$bar_logo, iconWidth =60, iconHeight =50)# Create the leaflet mapm <-leaflet() %>%# Add default OpenStreetMap map tilesaddTiles() %>%# Add markers to the map using the custom iconsaddMarkers(lng=data002$longitude, lat=data002$latitude, icon = leafIcons)# Display the mapm
3.2 Map 2: Markers with Popups
Popups are useful for displaying information when a user clicks on a marker.
Code
# Create a leaflet mapm <-leaflet(height=2000, width=2000) %>%# Add default OpenStreetMap map tilesaddTiles() %>%# Add popups that appear on click, showing the bar's nameaddPopups(lng=data002$longitude, lat=data002$latitude, data002$bar_name,options =popupOptions(closeButton =FALSE) # Optionally remove the close button )# Display the mapm
3.3 Map 3: Markers with Permanent Labels
Unlike popups, labels can be configured to be always visible, which is great for labeling a small number of locations directly on the map.
Code
# Create a leaflet mapm <-leaflet(height=2500, width=2000) %>%# Add default OpenStreetMap map tilesaddTiles() %>%# Add markers with labelsaddMarkers(lng=data002$longitude, lat=data002$latitude,label = data002$bar_name, # Set the label text# Configure label optionslabelOptions =labelOptions(noHide =TRUE, direction ="bottom", # Make label permanently visible below the markerstyle =list( # Apply custom CSS styling to the label"color"="red","font-family"="serif","font-style"="italic","box-shadow"="2px 2px rgba(0,0,0,0.25)","font-size"="12px","border-color"="rgba(0,0,0,0.5)" )))# Display the mapm
---title: "Mapping Hong Kong Whisky Bars"subtitle: "with leaflet"author: "Tony Duan"execute: warning: false error: falseformat: html: toc: true toc-location: right code-fold: show code-tools: true number-sections: true code-block-bg: true code-block-border-left: "#31BAE9" code-copy: true---This document demonstrates how to create an interactive map of whisky bars in Hong Kong using the `leaflet` and `tidygeocoder` packages in R. We will start by creating a dataset of whisky bars, then geocode their addresses to obtain latitude and longitude coordinates, and finally create a map with custom markers, popups, and labels.# Data and LibrariesFirst, we load the necessary R libraries and create a tibble containing the names, addresses, and logo URLs for a few whisky bars in Hong Kong.```{r}# Load necessary librarieslibrary(tidyverse) # For data manipulationlibrary(rvest) # For web scraping (not used here, but good to have)library(chromote) # For controlling headless Chrome (not used here)library(janitor) # For data cleaninglibrary(tidygeocoder) # For geocoding addresseslibrary(leaflet) # For creating interactive mapslibrary(sf) # For working with spatial data``````{r}# Create vectors for bar names, addresses, and logo URLsbar_name=c('Maltcask@太子','House Welley@中環','Casky@灣仔')bar_address=c('169 Sai Yeung Choi St N, Mong Kok,Hongkong','97 Wellington St, Central,Hongkong','20-24 Lockhart Road, Wan Chai,Hongkong')bar_logo=c('https://img.shoplineapp.com/media/image_clips/62f16f8d7df01b0025a82bad/original.png','https://static.wixstatic.com/media/2d390e_27d0b3da21a94459bccf741600782591~mv2.jpg/v1/fill/w_160,h_160,al_c,q_80,usm_0.66_1.00_0.01,enc_auto/Grey.jpg','https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcQsSelY2PRVxaxyU_Ydf9CHgtUrClzpxpqJ2g&s')# Create a tibble (a modern data frame)data001=tibble(bar_name,bar_address,bar_logo)```# Geocoding AddressesTo place the bars on a map, we need their geographical coordinates. The `tidygeocoder` package provides a convenient way to convert addresses into latitude and longitude using services like OpenStreetMap (osm).```{r}# Use the geocode function to find coordinates for the addressesdata002 =data001 %>%geocode(bar_address, method ='osm', lat = latitude , long = longitude)# Display the structure of the resulting data frameglimpse(data002)```# Creating the MapNow that we have the coordinates, we can create an interactive map using the `leaflet` package. We will explore three different ways to mark the locations on the map.## Map 1: Markers with Custom IconsWe can use the bar logos as custom icons for the markers on the map.```{r}# Create a list of custom icons from the logo URLsleafIcons <-icons(data002$bar_logo, iconWidth =60, iconHeight =50)# Create the leaflet mapm <-leaflet() %>%# Add default OpenStreetMap map tilesaddTiles() %>%# Add markers to the map using the custom iconsaddMarkers(lng=data002$longitude, lat=data002$latitude, icon = leafIcons)# Display the mapm ```## Map 2: Markers with PopupsPopups are useful for displaying information when a user clicks on a marker.```{r}# Create a leaflet mapm <-leaflet(height=2000, width=2000) %>%# Add default OpenStreetMap map tilesaddTiles() %>%# Add popups that appear on click, showing the bar's nameaddPopups(lng=data002$longitude, lat=data002$latitude, data002$bar_name,options =popupOptions(closeButton =FALSE) # Optionally remove the close button )# Display the mapm ```## Map 3: Markers with Permanent LabelsUnlike popups, labels can be configured to be always visible, which is great for labeling a small number of locations directly on the map.```{r}# Create a leaflet mapm <-leaflet(height=2500, width=2000) %>%# Add default OpenStreetMap map tilesaddTiles() %>%# Add markers with labelsaddMarkers(lng=data002$longitude, lat=data002$latitude,label = data002$bar_name, # Set the label text# Configure label optionslabelOptions =labelOptions(noHide =TRUE, direction ="bottom", # Make label permanently visible below the markerstyle =list( # Apply custom CSS styling to the label"color"="red","font-family"="serif","font-style"="italic","box-shadow"="2px 2px rgba(0,0,0,0.25)","font-size"="12px","border-color"="rgba(0,0,0,0.5)" )))# Display the mapm ```# Resource- [Leaflet for R](https://rstudio.github.io/leaflet/)- [Tidygeocoder Vignette](https://cran.r-project.org/web/packages/tidygeocoder/vignettes/tidygeocoder.html)